<template>
  <div class="main">
    <div>
      <!-- 二级路由插座 -->
      <router-view></router-view>
    </div>

    <van-tabbar
      v-model="activeTabIndex"
      inactive-color="#646566"
      active-color="#0C34BA"
      route
    >
      <van-tabbar-item
        v-for="(item, index) in tarbbarData"
        :key="index"
        :to="item.to"
        :badge="item.badge"
        :dot="item.dot"
      >
        <span>{{ item.title }}</span>
        <template #icon="props">
          <img :src="props.active ? item.activeIcon : item.inactiveIcon" />
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
export default {
  name: "Main",
  data() {
    return {
      activeTabIndex: 0,

      // shopbagCount: 0,
    };
  },
  computed: {
    //底部导航栏
    tarbbarData() {
      return [
        {
          title: "首页",
          activeIcon: require("../assets/imgs/home_active.png"),
          inactiveIcon: require("../assets/imgs/home.png"),
          to: { name: "Home" },
        },
        {
          title: "菜单",
          activeIcon: require("../assets/imgs/menu_active.png"),
          inactiveIcon: require("../assets/imgs/menu.png"),
          to: { name: "Menu" },
        },
        {
          title: "购物车",
          activeIcon: require("../assets/imgs/shopbag_active.png"),
          inactiveIcon: require("../assets/imgs/shopbag.png"),
          badge: this.$root.publicShopbagCount,
          to: { name: "Shopbag" },
        },
        {
          title: "我的",
          activeIcon: require("../assets/imgs/my_active.png"),
          inactiveIcon: require("../assets/imgs/my.png"),
          dot: true,
          to: { name: "My" },
        },
      ];
    },
  },
  created() {
    // console.log(this.shopbagCount);
    if (this.$root.publicShopbagCount === "") {
      this.getShopbagCount();
    }
  },
  methods: {
    //用户登录,查询购物车的商品总数量
    getShopbagCount() {
      //获取token
      let token = sessionStorage.getItem("token");

      this.axios({
        method: "get",
        url: "/shopcartCount",
        params: {
          token,
        },
      })
        .then((res) => {
          // console.log("用户登录", res);
          if (res.data.code === 700) {
            return;
          }
          if (res.data.code === 4000) {
            this.$root.publicShopbagCount = res.data.result;
            // console.log(this.$root.publicShopbagCount);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
  /deep/.van-info{
    background-color: #0c34ba;
  }
</style>